package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.util.JsonResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    IDestinationService destinationService;

    /**
     * 分页查询
     * @param model 模型
     * @param qo 查询条件
     * @return 页面
     */
    @RequestMapping("list")
    public Object list(Model model, @ModelAttribute("qo")DestinationQuery qo){
       // destinationService
        PageInfo<Destination> pageInfo = destinationService.listByPage(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Destination> toasts = destinationService.creatToasts(qo.getParentId());
        model.addAttribute("toasts", toasts);
        return "destination/list";
    }

    /**
     * 通过id删除数据
     * @param id id
     * @return 删除是否成功
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object deleteById(Long id) {
        destinationService.deleteById(id);
        return JsonResult.SUCCESS;
    }


    /**
     * 更改状态 是否热门
     * @param id id
     * @param hot 是不是热门
     * @return 结果
     */
    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue(Long id, boolean hot) {
        destinationService.changeHotValue(id, hot);
        return JsonResult.SUCCESS;
    }


    /**
     * 更改简介
     * @param id id
     * @param info 简介信息
     * @return 结果
     */
    @RequestMapping("setInfo")
    @ResponseBody
    public Object setInfo(Long id,String info){
        destinationService.updateInfoById(id, info);
        return JsonResult.SUCCESS;
    }
}
